﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EasyNet.Componets.Core.Mapper
{
    public class EasyNetMapper : IMapper
    {

        public TTarget Map<TSource, TTarget>(TSource source)
           where TTarget : class, new()
           where TSource : class
        {
            return EasyNetMapper<TSource, TTarget>.Map(source);
        }


        public TTarget Map<TSource, TTarget>(TSource source, TTarget target)
         where TTarget : class, new()
         where TSource : class
        {
            return EasyNetMapper<TSource, TTarget>.Map(source, target);
        }

    }

    internal class EasyNetMapper<Tsource, Ttarget>
        where Ttarget : class, new()
        where Tsource : class
    {
        private static readonly Func<Tsource, Ttarget, Ttarget> _mapActivater;

        private static readonly Func<Tsource, Ttarget> _mapperNewActivater;


        static EasyNetMapper()
        {
            _mapActivater = EasyNetMapperActivator.CreateMapActivater<Tsource, Ttarget>().Compile();

            _mapperNewActivater = EasyNetMapperActivator.CreateMapNewActivater<Tsource, Ttarget>().Compile();
        }
        /// <summary>
        /// 对象映射
        /// </summary>
        /// <param name="source"></param>
        /// <param name="target"></param>
        /// <returns></returns>
        public static Ttarget Map(Tsource source, Ttarget target)
        {
            return _mapActivater(source, target);
        }

        /// <summary>
        /// 创建对象并映射
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        [DebuggerStepThrough, DebuggerHidden]
        public static Ttarget Map(Tsource source)
        {
            return _mapperNewActivater(source);
        }

    }
}
